| Conditions | 10 |
| Paths | 10 |
| Total Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like gaaf.js ➔ ... ➔ fillInAddress often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | jQuery(function ($) { |
||
| 7 | function fillInAddress() { |
||
| 8 | var place = autocomplete.getPlace(); |
||
| 9 | var temp_state = ''; |
||
| 10 | |||
| 11 | for (var i = 0; i < place.address_components.length; i++) { |
||
| 12 | |||
| 13 | var address_type = place.address_components[i].types[0]; |
||
| 14 | |||
| 15 | switch (address_type) { |
||
| 16 | case 'route': |
||
| 17 | var val = place.address_components[i]['short_name']; |
||
| 18 | document.getElementById('wpinv_address').value = val; |
||
| 19 | break; |
||
| 20 | case 'locality': |
||
| 21 | var val = place.address_components[i]['long_name']; |
||
| 22 | document.getElementById('wpinv_city').value = val; |
||
| 23 | break; |
||
| 24 | case 'administrative_area_level_1': |
||
| 25 | var val = place.address_components[i]['short_name']; |
||
| 26 | temp_state = val; |
||
| 27 | break; |
||
| 28 | case 'country': |
||
| 29 | var val = place.address_components[i]['short_name']; |
||
| 30 | |||
| 31 | $('#wpinv_country').val(val); |
||
| 32 | |||
| 33 | var elB = $('#wpinv-address'); |
||
| 34 | var elF = $('#wpinv-fields'); |
||
| 35 | data = { |
||
| 36 | action: 'wpinv_get_states_field', |
||
| 37 | country: val, |
||
| 38 | field_name: 'wpinv_state', |
||
| 39 | }; |
||
| 40 | |||
| 41 | if(elB.length){ |
||
| 42 | var $this = $('#wpinv_country', elB); |
||
| 43 | |||
| 44 | $this.closest('.gdmbx-row').find('.wpi-loader').show(); |
||
| 45 | $('#wpinv_state', elB).css({ |
||
| 46 | 'opacity': '.5' |
||
| 47 | }); |
||
| 48 | $.post(ajaxurl, data, function(response) { |
||
| 49 | if ('nostates' === response || '' == temp_state) { |
||
| 50 | var text_field = '<input type="text" value="' + temp_state + '" id="wpinv_state" name="wpinv_state" />'; |
||
| 51 | $('#wpinv_state', elB).replaceWith(text_field); |
||
| 52 | } else { |
||
| 53 | $('#wpinv_state', elB).replaceWith(response); |
||
| 54 | $('#wpinv_state', elB).find('option[value="' + temp_state + '"]').attr('selected', 'selected'); |
||
| 55 | $('#wpinv_state', elB).find('option[value=""]').remove(); |
||
| 56 | } |
||
| 57 | |||
| 58 | $('#wpinv_state', elB).addClass('gdmbx2-text-large'); |
||
| 59 | if (typeof $this.data('change') === '1') { |
||
| 60 | $('#wpinv_state', elB).change(); |
||
| 61 | } else { |
||
| 62 | window.wpiConfirmed = true; |
||
| 63 | $('#wpinv-recalc-totals').click(); |
||
| 64 | window.wpiConfirmed = false; |
||
| 65 | } |
||
| 66 | |||
| 67 | $this.closest('.gdmbx-row').find('.wpi-loader').hide(); |
||
| 68 | $('#wpinv_state', elB).css({ |
||
| 69 | 'opacity': '1' |
||
| 70 | }); |
||
| 71 | }); |
||
| 72 | } else if(elF.length){ |
||
| 73 | $('.wpinv_errors').remove(); |
||
| 74 | wpinvBlock(jQuery('#wpinv_state_box')); |
||
| 75 | var $this = $('#wpinv_country', elF); |
||
| 76 | $.post(ajaxurl, data, function(response) { |
||
| 77 | if ('nostates' === response) { |
||
| 78 | var text_field = '<input type="text" required="required" class="wpi-input required" id="wpinv_state" name="wpinv_state">'; |
||
| 79 | $('#wpinv_state', elF).replaceWith(text_field); |
||
| 80 | } else { |
||
| 81 | $('#wpinv_state', elF).replaceWith(response); |
||
| 82 | $('#wpinv_state', elF).find('option[value="' + temp_state + '"]').attr('selected', 'selected'); |
||
| 83 | var changeState = function() { |
||
| 84 | wpinv_recalculate_taxes(temp_state); |
||
| 85 | }; |
||
| 86 | $("#wpinv_state", elF).unbind("change", changeState); |
||
| 87 | $("#wpinv_state", elF).bind("change", changeState); |
||
| 88 | } |
||
| 89 | $('#wpinv_state', elF).find('option[value=""]').remove(); |
||
| 90 | $('#wpinv_state', elF).addClass('form-control wpi-input required'); |
||
| 91 | }).done(function(data) { |
||
| 92 | jQuery('#wpinv_state_box').unblock(); |
||
| 93 | wpinv_recalculate_taxes(); |
||
| 94 | }); |
||
| 95 | } |
||
| 96 | |||
| 97 | break; |
||
| 98 | case 'postal_code': |
||
| 99 | var val = place.address_components[i]['short_name']; |
||
| 100 | document.getElementById('wpinv_zip').value = val; |
||
| 101 | break; |
||
| 102 | default: |
||
| 103 | break; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | }); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.